package ishaportalunittests.apis.geolocation;

import static org.junit.Assert.assertTrue;
import gwtappcontainer.server.apis.content.ContentAPI;
import gwtappcontainer.server.apis.geolocation.GeoLocationAPI;
import gwtappcontainer.shared.apis.APIResponse;
import gwtappcontainer.shared.apis.APIResponse.Status;
import ishaportalunittests.helpers.APITestHelper;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class GeoLocationAPITest {
	
private final APITestHelper helper = new APITestHelper();
	
	@Before
	public void setUp() {
		helper.setUp();
	}
	
	@After 
	public void tearDown() {
		helper.tearDown();
	}
	
	@Test
	public void canGetCityContent() {
		ContentAPI contentAPI = new ContentAPI();
		
		contentAPI.addNewTag("ishakriyaschedule_mumbai", helper.loginAsDeveloper());
		contentAPI.setContent("content for mumbai", "ishakriyaschedule_mumbai", true, helper.loginAsPortalAdmin());
		
		GeoLocationAPI geoApi = new GeoLocationAPI();
		String content = geoApi.getContentForCity("ishakriyaschedule", "mumbai").object.toString();
		assertTrue(content.equals("content for mumbai"));								
	}
	
	@Test
	public void cityAndTagAreCaseInsensitive() {
		ContentAPI contentAPI = new ContentAPI();
		
		contentAPI.addNewTag("ishakriyaschedule_mumbai", helper.loginAsDeveloper());
		contentAPI.setContent("content for mumbai", "ishakriyaschedule_mumbai", true, helper.loginAsPortalAdmin());
		
		GeoLocationAPI geoApi = new GeoLocationAPI();
		String content = geoApi.getContentForCity("ishakriyASchedule", "mUMBAi").object.toString();
		assertTrue(content.equals("content for mumbai"));
	}
	
	@Test
	public void canGetDefaultContentIfCityContentNotAvailable() {
		ContentAPI contentAPI = new ContentAPI();
		
		contentAPI.addNewTag("ishakriyaschedule_default", helper.loginAsDeveloper());
		contentAPI.setContent("content for default", "ishakriyaschedule_default", true, helper.loginAsPortalAdmin());
		
		GeoLocationAPI geoApi = new GeoLocationAPI();
		String content = geoApi.getContentForCity("ishakriyASchedule", "mUMBAi").object.toString();
		assertTrue(content.equals("content for default"));
	}
	
	@Test
	public void resoureNotFoundErrorIfBothDefaultContentAndCityContentNotAvailable() {
		GeoLocationAPI geoApi = new GeoLocationAPI();
		
		APIResponse response = geoApi.getContentForCity("ishakriyaschedule", "mumbai");
		assertTrue(response.statusCode == Status.ERROR_RESOURCE_DOES_NOT_EXIST);	
	}
	
	@Test
	public void canGetDefaultContentIfCityContentNotPublished() {
		ContentAPI contentAPI = new ContentAPI();
		
		//add content for mumbai but don't publish it
		contentAPI.addNewTag("ishakriyaschedule_mumbai", helper.loginAsDeveloper());
		contentAPI.setContent("content for mumbai", "ishakriyaschedule_mumbai", 
				false, helper.loginAsPortalAdmin());
		
		//add content for default
		contentAPI.addNewTag("ishakriyaschedule_default", helper.loginAsDeveloper());
		contentAPI.setContent("content for default", "ishakriyaschedule_default", 
				true, helper.loginAsPortalAdmin());
		
		GeoLocationAPI api = new GeoLocationAPI();
		String content = api.getContentForCity("ishakriyaSchedule", "mumbai").object.toString();
		assertTrue(content.equals("content for default"));
		
	}
}
